package com.ysw.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: yu_sw
 * @Description: com.ysw.config
 * @date:2022/5/17 21:39
 */
@Configuration
public class ThreadPoolConfig {
    @Bean("corePoll")
    public ThreadPoolTaskExecutor myThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //此方法返回可用处理器的虚拟机的最大数量;
//        int core = Runtime.getRuntime().availableProcessors();
        int core = 3;
        //设置核心线程数
        executor.setCorePoolSize(core);
        //设置最大线程数
        executor.setMaxPoolSize(core * 10 + core);
        //除核心线程外的线程存活时间
        executor.setKeepAliveSeconds(3);
        //缓冲队列
        executor.setQueueCapacity(core);
        executor.setThreadNamePrefix("thread-execute");
        //设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}
